package annotation

import (
	"gopkg.openfuyao.cn/bkecommon"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
	BKEClusterDryRunAnnotationKey          = "bke.bocloud.com/dryrun"
	BKEClusterPauseAnnotationKey           = "bke.bocloud.com/pause"
	CommandReconciledAnnotationKey         = "bke.bocloud.com/command-reconciled"
	ClusterCollectdAnnotationKey           = "bke.bocloud.com/collectd"
	KONKFullManagementClusterAnnotationKey = "bke.bocloud.com/full-management"
	LastUpdateConfigurationAnnotationKey   = "bke.bocloud.com/last-update-configuration"
	AtPrecheckPhaseAnnotationKey           = "bke.bocloud.com/at-precheck-phase"
	BKEMachineProviderIDAnnotationKey      = "bke.bocloud.com/providerID"
	EtcdAdvertiseClientUrlsAnnotationKey   = "bkeagent.bocloud.com/etcd.advertise-client-urls"

	StatusRecordAnnotationKey = "bke.bocloud.com/status-record"
)

// feature gates annotation key
const (
	// RetryAnnotationKey is the annotation key for retry
	RetryAnnotationKey = "bke.bocloud.com/retry"

	// DeepRestoreNodeAnnotationKey is the annotation key for deep restore node
	DeepRestoreNodeAnnotationKey = "bke.bocloud.com/deep-restore-node"
	// MasterSchedulableAnnotationKey is the annotation key for master schedulable
	MasterSchedulableAnnotationKey = "bke.bocloud.com/master-schedulable"
	// DeleteIgnoreNamespaceAnnotationKey is the annotation key for delete ignore namespace
	DeleteIgnoreNamespaceAnnotationKey = "bke.bocloud.com/ignore-namespace-delete"
	// DeleteIgnoreTargetClusterAnnotationKey is the annotation key for delete ignore target cluster
	DeleteIgnoreTargetClusterAnnotationKey = "bke.bocloud.com/ignore-target-cluster-delete"
	// AppointmentDeletedNodesAnnotationKey is the annotation key for appointment deleted nodes
	AppointmentDeletedNodesAnnotationKey = "bke.bocloud.com/appointment-deleted-nodes"
	// AppointmentAddNodesAnnotationKey is the annotation key for appointment add nodes
	AppointmentAddNodesAnnotationKey = "bke.bocloud.com/appointment-add-nodes"

	// NodeBootWaitTimeOutAnnotationKey is the annotation key for node boot wait timeout
	NodeBootWaitTimeOutAnnotationKey = "bke.bocloud.com/node-boot-wait-timeout"
	// ClusterTrackerHealthyCheckFailedAnnotationKey is the annotation key for cluster tracker healthy check faild
	ClusterTrackerHealthyCheckFailedAnnotationKey = "bke.bocloud.com/cluster-tracker-healthy-check-failed"

	// AddonBootWaitTimeOutAnnotationKey is the annotation key for addon boot wait timeout
	AddonBootWaitTimeOutAnnotationKey = "bke.bocloud.com/addon-boot-wait-timeout"
)

func SetBKEClusterDefaultAnnotation(bkeCluster client.Object) {
	if _, ok := HasAnnotation(bkeCluster, DeleteIgnoreTargetClusterAnnotationKey); !ok {
		SetAnnotation(bkeCluster, DeleteIgnoreTargetClusterAnnotationKey, "true")
	}
	if _, ok := HasAnnotation(bkeCluster, DeleteIgnoreNamespaceAnnotationKey); !ok {
		SetAnnotation(bkeCluster, DeleteIgnoreNamespaceAnnotationKey, "true")
	}
	if _, ok := HasAnnotation(bkeCluster, bkecommon.BKEAgentListenerAnnotationKey); !ok {
		SetAnnotation(bkeCluster, bkecommon.BKEAgentListenerAnnotationKey, bkecommon.BKEAgentListenerCurrent)
	}
	if _, ok := HasAnnotation(bkeCluster, bkecommon.BKEClusterFromAnnotationKey); !ok {
		SetAnnotation(bkeCluster, bkecommon.BKEClusterFromAnnotationKey, "")
	}
	if _, ok := HasAnnotation(bkeCluster, DeepRestoreNodeAnnotationKey); !ok {
		SetAnnotation(bkeCluster, DeepRestoreNodeAnnotationKey, "true")
	}
	if _, ok := HasAnnotation(bkeCluster, MasterSchedulableAnnotationKey); !ok {
		SetAnnotation(bkeCluster, MasterSchedulableAnnotationKey, "false")
	}
	//if _, ok := HasAnnotation(bkeCluster, BKECLusterHealthCheckIntervalAnnotationKey); !ok {
	//	SetAnnotation(bkeCluster, BKECLusterHealthCheckIntervalAnnotationKey, "24h")
	//}
	if _, ok := HasAnnotation(bkeCluster, NodeBootWaitTimeOutAnnotationKey); !ok {
		SetAnnotation(bkeCluster, NodeBootWaitTimeOutAnnotationKey, "10m")
	}
}

func HasAnnotation(obj client.Object, key string) (string, bool) {
	annotations := obj.GetAnnotations()
	if annotations == nil {
		return "", false
	}
	v, ok := annotations[key]
	return v, ok
}

func SetAnnotation(obj client.Object, key, value string) {
	annotations := obj.GetAnnotations()
	if annotations == nil {
		annotations = map[string]string{}
	}
	annotations[key] = value
	obj.SetAnnotations(annotations)
}

func RemoveAnnotation(obj client.Object, key string) {
	annotations := obj.GetAnnotations()
	if annotations == nil {
		return
	}
	delete(annotations, key)
	obj.SetAnnotations(annotations)
}

func BKENormalEventAnnotation() map[string]string {
	return map[string]string{
		bkecommon.BKEEventAnnotationKey: "",
	}
}

func BKEFinishEventAnnotation() map[string]string {
	return map[string]string{
		bkecommon.BKEEventAnnotationKey:       "",
		bkecommon.BKEFinishEventAnnotationKey: "",
	}
}
